/*
 * @(#)HttpClientUtil.java $version 2011. 10. 14
 * 
 * Copyright 2011 NHN Corp. All rights Reserved.
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.hangame.mbl.util;

import java.io.IOException;		

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


// TODO: Auto-generated Javadoc
/**
 * The Class HttpClientUtil.
 * 
 * @author 양현규
 */
public class HttpClientUtil {
	
	/** The LOG. */
	private static final Log LOG = LogFactory.getLog(HttpClientUtil.class);
	
	/**
	 * Gets the response body get.
	 * 
	 * @param url the url
	 * @param encoding the encoding
	 * @param timeOutSec the time out sec
	 * @return the response body get
	 * @throws HttpException the http exception
	 */
	public static String[] getResponseBodyGet(String url, String encoding, int timeOutSec) throws HttpException {
		String[] result = new String[2];
		String responseBody = "";
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(url);
			httpGet.addHeader("Content-type", "application/json");
			httpGet.addHeader("Accept", "application/json");
			
			int statusCode = 0;
			try {
				HttpResponse httpResponse = client.execute(httpGet);
				statusCode = httpResponse.getStatusLine().getStatusCode();
				
				if (statusCode == HttpStatus.SC_OK) {
					responseBody = EntityUtils.toString(httpResponse.getEntity());
				} else {
					LOG.debug(statusCode);
				}
			} catch (IOException e) {
				LOG.error("Error occurred in calling httpclient:" + e.getMessage());
				return result;
			} finally {
				client.getConnectionManager().shutdown();
			}
			
			result[0] = String.valueOf(statusCode);
			result[1] = responseBody;
			return result;
		} catch (Exception e) {
			LOG.error("Error occurred in calling httpclient:" + e.getMessage());
			return result;
		}
	}
	
	/**
	 * Gets the response body post.
	 * 
	 * @param url the url
	 * @param params the params
	 * @param encoding the encoding
	 * @param timeOutSecs the time out secs
	 * @return the response body post
	 * @throws HttpException the http exception
	 */
	public static String[] getResponseBodyPost(String url, JSONObject params, String encoding, int timeOutSecs) throws HttpException {
		String[] result = new String[2];

		try {
			HttpClient client = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(url);
			httpPost.addHeader("Content-type", "application/json");
			httpPost.addHeader("Accept", "application/json");
			
			httpPost.setEntity(new ByteArrayEntity(params.toString().getBytes("UTF-8")));

			int statusCode = 0;
			String responseBody = "";
			try {
				HttpResponse httpResponse = client.execute(httpPost);
				statusCode = httpResponse.getStatusLine().getStatusCode();
				
				if (statusCode == HttpStatus.SC_OK) {
					responseBody = EntityUtils.toString(httpResponse.getEntity());
				} else {
					LOG.debug(statusCode);
				}
			} catch (IOException e) {
				LOG.error("Error occurred in calling httpclient:" + e.getMessage());
				return result;
			} finally {
				client.getConnectionManager().shutdown();
			}
			
			result[0] = String.valueOf(statusCode);
			result[1] = responseBody;
			return result;
		} catch (Exception e) {
			LOG.error("Error occurred in calling httpclient:" + e.getMessage());
			return result;
		}
	}
	
	/**
	 * Gets the response body post.
	 * 
	 * @param url the url
	 * @param params the params
	 * @param encoding the encoding
	 * @param timeOutSecs the time out secs
	 * @return the response body post
	 * @throws HttpException the http exception
	 */
	public static String[] getResponseBodyPut(String url, JSONObject params, String encoding, int timeOutSecs) throws HttpException {
		String[] result = new String[2];

		try {
			HttpClient client = new DefaultHttpClient();
			HttpPut httpPut = new HttpPut(url);
			httpPut.addHeader("Content-type", "application/json");
			
			httpPut.setEntity(new ByteArrayEntity(params.toString().getBytes("UTF-8")));

			int statusCode = 0;
			String responseBody = "";
			try {
				HttpResponse httpResponse = client.execute(httpPut);
				statusCode = httpResponse.getStatusLine().getStatusCode();
				
				if (statusCode != HttpStatus.SC_OK) {
					responseBody = httpResponse.getStatusLine().getReasonPhrase();
				} else {
					LOG.debug(statusCode);
				}
			} catch (IOException e) {
				LOG.error("Error occurred in calling httpclient:" + e.getMessage());
				return result;
			} finally {
				client.getConnectionManager().shutdown();
			}

			result[0] = String.valueOf(statusCode);
			result[1] = responseBody;
			return result;
		} catch (Exception e) {
			LOG.error("Error occurred in calling httpclient:" + e.getMessage());
			return result;
		}
	}
}
